package com.keyancompany.zhongbaopingtai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.model.TalkList;

import java.util.List;

/**
 * Created by Gia on 2016/9/2.
 */
public class DiscussAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>implements View.OnClickListener{
    //上拉加载更多
    public static final int  PULLUP_LOAD_MORE=0;
    //正在加载中
    public static final int  LOADING_MORE=1;
    //上拉加载更多状态-默认为0
    private int load_more_status=0;
    private LayoutInflater mInflater;
    private List<TalkList> mTalks=null;
    private static final int TYPE_ITEM = 0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //顶部FootView

    // 声明一个这个接口的变量
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public DiscussAdapter(Context context, List<TalkList> list){
        this.mInflater=LayoutInflater.from(context);
        this.mTalks = list;
    }
    /**
     * item显示类型
     * @param parent
     * @param viewType
     * @return
     */
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //进行判断显示类型，来创建返回不同的View
        if(viewType==TYPE_ITEM){
            View view=mInflater.inflate(R.layout.item_discuss,parent,false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            ItemViewHolder itemViewHolder=new ItemViewHolder(view);

            //将创建的View注册点击事件
            view.setOnClickListener(this);

            return itemViewHolder;
        }else if(viewType==TYPE_FOOTER){
            View foot_view=mInflater.inflate(R.layout.recycler_load_more_layout,parent,false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            FootViewHolder footViewHolder=new FootViewHolder(foot_view);
            return footViewHolder;
        }


        return null;
    }

    /**
     * 数据的绑定显示
     * @param holder
     * @param position
     */
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder) {
            ((ItemViewHolder)holder).talk_content.setText(Html.fromHtml(mTalks.get(position).getTalkContent()));
            ((ItemViewHolder)holder).state.setText("状态:"+mTalks.get(position).getState());
            ((ItemViewHolder)holder).talk_title.setText(mTalks.get(position).getTalkTitle());
            ((ItemViewHolder)holder).username.setText("创建者:"+mTalks.get(position).getUsername());
            ((ItemViewHolder)holder).team_code.setText("所在团队:"+mTalks.get(position).getTeamCode());
            ((ItemViewHolder)holder).talk_time.setText("讨论时间:"+mTalks.get(position).getTalkTime());

            holder.itemView.setTag(mTalks.get(position));


        }else if(holder instanceof FootViewHolder){
            FootViewHolder footViewHolder=(FootViewHolder)holder;            switch (load_more_status){
                case PULLUP_LOAD_MORE:
                    footViewHolder.foot_view_item_tv.setText("上拉加载更多...");
                    break;
                case LOADING_MORE:
                    footViewHolder.foot_view_item_tv.setText("正在加载更多数据...");
                    break;
            }
        }
    }

    /**
     * 进行判断是普通Item视图还是FootView视图
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }
    @Override
    public int getItemCount() {
        return mTalks.size()+1;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        public TextView state;
        public TextView talk_title;
        public TextView talk_content;
        public TextView username;
        public TextView team_code;
        public TextView talk_time;


        public ItemViewHolder(View view){
            super(view);
            state = (TextView)view.findViewById(R.id.state);
            talk_title = (TextView)view.findViewById(R.id.talk_title);
            talk_content = (TextView)view.findViewById(R.id.talk_content);
            username = (TextView)view.findViewById(R.id.username);
            team_code = (TextView)view.findViewById(R.id.team_code);
            talk_time = (TextView)view.findViewById(R.id.talk_time);
        }
    }
    /**
     * 底部FootView布局
     */
    public static class FootViewHolder extends  RecyclerView.ViewHolder{
        private TextView foot_view_item_tv;
        public FootViewHolder(View view) {
            super(view);
            foot_view_item_tv=(TextView)view.findViewById(R.id.foot_view_item_tv);
        }
    }

    //添加数据
    public void addItem(List<TalkList> newDatas) {
        //mTitles.add(position, data);
        //notifyItemInserted(position);
        newDatas.addAll(mTalks);
        mTalks.removeAll(mTalks);
        mTalks.addAll(newDatas);
        notifyDataSetChanged();
    }

    public void addMoreItem(List<TalkList> newDatas) {
        mTalks.addAll(newDatas);
        notifyDataSetChanged();
    }

    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     * @param status
     */
    public void changeMoreStatus(int status){
        load_more_status=status;
        notifyDataSetChanged();
    }


    //define interface
     public static interface OnRecyclerViewItemClickListener {
          void onItemClick(View view , TalkList data);
       }

    @Override
   public void onClick(View v) {
         if (mOnItemClickListener != null) {
          //注意这里使用getTag方法获取数据
                mOnItemClickListener.onItemClick(v,(TalkList)v.getTag());
          }
        }

     public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
             this.mOnItemClickListener = listener;
     }



}